import { defineStore } from 'pinia';
import * as CONSTANT from '@/utils/constant.js';
import _ from 'lodash';

const useUserStore = defineStore({
    id: 'user_info',
    state: () => {
        const user_info = uni.getStorageSync(CONSTANT.USER_INFO_KEY) || {};
        const token = uni.getStorageSync(CONSTANT.USER_TOKEN_KEY) || '';
        return {
            user_info,
            token,
            user_id: user_info?.user_id || '',
            user_birth: user_info?.user_birth || '',
            user_avatar: user_info?.user_avatar || '',
            user_mobile: user_info?.user_mobile || '',
            user_name: user_info?.user_name || '',
            user_profile: user_info?.user_profile || '',
            user_sex: user_info?.user_sex || 0
        };
    },
    actions: {
        /**
         * 修改用户信息数据
         * @param {Object} data 用户信息数据
         */
        modifyUserData(data) {
            Object.keys(data).includes('user_token') && this.modifyUserToken(data.user_token);
            if (Object.keys(data).includes('user_token')) delete data.token;
            uni.setStorageSync(CONSTANT.USER_INFO_KEY, data);
            this.user_info = data;
            this.$reset();
        },
        /**
         * 修改用户token
         * @param {String} data 用户token
         */
        modifyUserToken(data) {
            uni.setStorageSync(CONSTANT.USER_TOKEN_KEY, data);
            this.token = data;
        },
        resetUserStore() {
            uni.clearStorageSync();
            this.$reset();
        }
    }
});
export default useUserStore;
